﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit.Electric.Common;

namespace HYCableTrayConnection
{
	public class HYAutomaticConnect : HYCmdEntry
	{
		public HYAutomaticConnect(ExternalCommandData commandData) : base(commandData)
		{
		}

		public void Execute()
		{
			try
			{
				IList<CableTray> list = new List<CableTray>();
				ISelectionFilter selectionFilter = new RevitFilter.SelectionCableTrayFilter();
				HYAutomaticConnection hyautomaticConnection = new HYAutomaticConnection();
				IList<Element> list2 = new List<Element>();
				int num = 0;
				for (;;)
				{
					if (num != 0)
					{
						RevitVersionFuncs.ClearSelection(this.m_sel);
						list2.Clear();
						list.Clear();
					}
					num++;
					if (this.m_sel.GetElementIds().Count > 0)
					{
						IEnumerator<ElementId> enumerator = this.m_sel.GetElementIds().GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								ElementId elementId = enumerator.Current;
								list2.Add(this.m_doc.GetElement(elementId));
							}
							goto IL_A8;
						}
						goto IL_96;
					}
					goto IL_96;
					IL_A8:
					foreach (Element element in list2)
					{
						if (element is CableTray)
						{
							list.Add(element as CableTray);
						}
					}
					if (list == null || list.Count < 2 || list.Count > 4)
					{
						TaskDialog.Show("盈建科提示", "连接错误，请选择2到4根桥架进行连接。");
						continue;
					}
					if (!hyautomaticConnection.NewAutomaticConnect(list))
					{
						TaskDialog.Show("盈建科提示", "连接错误，请检查桥架位置。");
						continue;
					}
					continue;
					IL_96:
					list2 = this.m_sel.PickElementsByRectangle(selectionFilter, "请选择需要连接的桥架");
					goto IL_A8;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
			}
			catch (MyException ex)
			{
				TaskDialog.Show("盈建科提示", "连接错误，" + ex.Message + "。");
			}
			catch (Exception)
			{
				TaskDialog.Show("盈建科提示", "连接错误，请检查桥架位置。");
			}
		}
	}
}
